<?php

namespace App\Listeners;

use Firebase\JWT\JWT;
use Swof\Event\Listener;
use Swof\Http\WebSocketRequest;

class CloseListener extends Listener
{
    protected $name = 'ws.close';

    public function handle($args = [])
    {
        $wsServer = $args[0];
        $swoServer = $args[1];
        $fd = $args[2];

        $swoRequest = WebSocketRequest::get($fd)['request'];

        $token = $swoRequest->header['sec-websocket-protocol'];

        $config = config('server.route.jwt');

        $jwt = JWT::decode($token, $config['key'], $config['algorithm']);

        $wsServer->getRedis()->hdel($config['key'], $jwt->data->uid);
    }
}